package com.hibernate.rcache.strategy;

import com.hibernate.rcache.region.RcacheEntityRegion;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.EntityRegion;
import org.hibernate.cache.access.EntityRegionAccessStrategy;
import org.hibernate.cache.access.SoftLock;
import org.hibernate.cfg.Settings;

public class ReadOnlyRcacheEntityRegionAccessStrategy implements EntityRegionAccessStrategy {

    protected final RcacheEntityRegion region;
    protected final Settings settings;

    public ReadOnlyRcacheEntityRegionAccessStrategy(RcacheEntityRegion rcacheEntityRegion, Settings settings) {
        region=rcacheEntityRegion;
        this.settings = settings;
    }

    public EntityRegion getRegion() {

        return region;
    }

    public boolean insert(Object key, Object value, Object version) throws CacheException {
        return false;
    }

    public boolean afterInsert(Object key, Object value, Object version) throws CacheException {
        region.put( key, value );
        return true;
    }

    public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) throws CacheException {

        return false;
    }

    public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) throws CacheException {

        return false;
    }

    public Object get(Object key, long txTimestamp) throws CacheException {

        return region.get(key);
    }

    public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version) throws CacheException {

        return false;
    }

    public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride) throws CacheException {
        if (minimalPutOverride&&region.contains(key)){
            return false;
        }else {
            region.put(key,value);
            return true;
        }

    }

    public SoftLock lockItem(Object key, Object version) throws CacheException {
        return null;
    }

    public SoftLock lockRegion() throws CacheException {
        return null;
    }


    public void unlockItem(Object key, SoftLock lock) throws CacheException {
        evict( key );
    }

    public void unlockRegion(SoftLock lock) throws CacheException {
        region.clear();
    }

    public void remove(Object key) throws CacheException {

        region.remove( key );
    }

    public void removeAll() throws CacheException {

        region.clear();
    }

    public void evict(Object key) throws CacheException {

        region.remove( key );

    }

    public void evictAll() throws CacheException {
        region.clear();
    }
}
